<template>
    <div class="ele-body">
      <el-card shadow="never">
        <!-- 搜索表单 -->
        <el-form
          :model="where"
          label-width="100px"
          class="ele-form-search"
          @keyup.enter.native="reload"
          @submit.native.prevent>
          <el-row :gutter="15">
            <el-col :lg="6" :md="12">
                <el-form-item label="平台医院代码:">
                  <el-input
                    clearable
                    v-model="form.yybm"
                    placeholder=""/>
                </el-form-item>
              </el-col>
              <el-col :lg="6" :md="12">
                <el-form-item label="平台医院名称:">
                  <el-input
                    clearable
                    v-model="form.yymc"
                    placeholder=""/>
                </el-form-item>
              </el-col>
              <el-col :lg="6" :md="12">
              <div class="ele-form-actions">
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  class="ele-btn-icon"
                  size="small"
                  @click="reload">查询
                </el-button>
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  class="ele-btn-icon"
                  size="small"
                  @click="editRate">修改
                </el-button>
                <el-button @click="reset" size="small">重置</el-button>
              </div>
            </el-col>
          </el-row>
          <el-row :gutter="15">
              <el-col :lg="4" :md="12">
                <el-form-item label="配送点编码:">
                  <el-input
                    clearable
                    disabled
                    v-model="form.psdbm"
                    placeholder=""/>
                </el-form-item>
              </el-col>
              <el-col :lg="8" :md="12">
                <el-form-item label="配送点名称:">
                  <el-input
                    disabled
                    v-model="form.psdz"
                    placeholder=""/>
                </el-form-item>
              </el-col>
          </el-row>
          <el-row>
            <el-col :lg="8" :md="12">
                <el-form-item label="NC客户编码:">
                  <el-input style="padding-top: 5px;"
                    disabled
                    v-model="form.custcode"
                    placeholder="">
                    <template slot="append">
                      <el-button
                      type="primary"
                      icon="el-icon-search"
                      class="ele-btn-icon"
                      size="small"
                      @click="openSelect">选择
                    </el-button>
                    </template>
                    </el-input>
                </el-form-item>
              </el-col>
              <el-col :lg="8" :md="12">
                <el-form-item label="NC客户名称:">
                  <el-input
                    disabled
                    v-model="form.custname"
                    placeholder=""/>
                </el-form-item>
              </el-col>
              <el-col :lg="4" :md="12">
                <el-form-item label="">
                  <el-checkbox v-model="setFlag">未设置</el-checkbox>
                </el-form-item>
              </el-col>
          </el-row>
          <el-row>
            <el-col :lg="12" :md="12">
                <el-form-item label="ERP配送地址:" label-width="110px">
                  <el-input
                    disabled
                    v-model="form.custaddrname"
                    placeholder=""/>
                </el-form-item>
              </el-col>
              <el-col :lg="4" :md="12">
                <el-form-item label="业务员">
                    <el-input
                    v-model="form.ywy"
                    placeholder=""/>
                </el-form-item>
              </el-col>
            <el-col :lg="5" :md="12">
              <el-form-item label="ERP出库仓库">
                <el-select v-model="form.erpwarehouse" placeholder="请选择" class="ele-block" clearable>
                  <el-option v-for="item in warehouseList" :key="item.code" :label="item.name" :value="item.code"/>
                </el-select>
              </el-form-item>
            </el-col>

          </el-row>

        </el-form>
        <!-- 数据表格 -->
        <ele-pro-table
          ref="table"
          :where="where"
          :datasource="url"
          :columns="columns"
          :selection.sync="selection"
          :pageSize="100"
          @row-click="handleRowClick"
          height="calc(100vh - 315px)">
          <!-- 表头工具栏 -->
          <template slot="toolbar" v-if="false">
            <el-button
              size="small"
              type="primary"
              icon="el-icon-plus"
              class="ele-btn-icon"
              @click="openEdit(null)"
              v-if="permission.includes('sys:c37infypyhjl:add')">添加
            </el-button>
            <el-button
              size="small"
              type="danger"
              icon="el-icon-delete"
              class="ele-btn-icon"
              @click="removeBatch"
              v-if="permission.includes('sys:c37infypyhjl:dall')">删除
            </el-button>
          </template>
          <!-- 操作列 -->
          <template slot="action" slot-scope="{row}">
            <el-link
              type="primary"
              :underline="false"
              icon="el-icon-edit"
              @click="openEdit(row)"
              v-if="permission.includes('sys:c37infypyhjl:edit')">修改
            </el-link>
            <el-popconfirm
              class="ele-action"
              title="确定要删除此养护记录表吗？"
              @confirm="remove(row)">
              <el-link
                type="danger"
                slot="reference"
                :underline="false"
                icon="el-icon-delete"
                v-if="permission.includes('sys:c37infypyhjl:delete')">删除
              </el-link>
            </el-popconfirm>
          </template>
        </ele-pro-table>
      </el-card>
      <!-- 编辑弹窗 -->
      <CustAddrList
        :dataItem="current"
        :visible.sync="showEdit"
        @done="setInvcode"/>
    </div>
  </template>

  <script>
  import { mapGetters } from "vuex";
  import moment from 'moment';
  import CustAddrList from './frmCustomerAddrList.vue';
  //import CustAddrList from './frmCustomer56AddrList.vue';

  export default {
    name: 'HospitalAddr',
    components: {CustAddrList},
    computed: {
      ...mapGetters(["permission"]),
    },
    data() {
      return {
        // 表格数据接口
        url: '/ygorder/ygptaddr',
        // 表格列配置
        columns: [
          // {
          //   columnKey: 'selection',
          //   type: 'selection',
          //   width: 45,
          //   align: 'center',
          //   fixed: "left"
          // },
          {
            prop: 'dbilldate',
            label: '单据日期',
            sortable: 'custom',
            showOverflowTooltip: true,
            minWidth: 160,
            align: 'center',
            formatter: (row, column, cellValue) => {
              return this.dateFormat(row,column,cellValue);
            }
          },
          {
            prop: 'yybm',
            label: '平台医院编码',
            showOverflowTooltip: true,
            minWidth: 180,
            align: 'center'
          },
          {
            prop: 'yymc',
            label: '平台医院名称',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'psdbm',
            label: '配送点编码',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'psdz',
            label: '配送地址',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'custaddrname',
            label: 'ERP配送地址',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'erpwarehouse',
            label: 'ERP出库仓库',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'coperator',
            label: '操作人',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'ywy',
            label: '业务员',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'custcode',
            label: '客户编码',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'custname',
            label: '客户名称',
            showOverflowTooltip: true,
            minWidth: 180,
            align: 'center'
          },
          {
            prop: 'pkcustaddr',
            label: 'pkcustaddr',
            showOverflowTooltip: true,
            minWidth: 160,
            align: 'center'
          },
        ],
        // 表格搜索条件
        where: {},
        form: {},
        // 表格选中数据
        selection: [],
        warehouseList:[],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
        setFlag:false,
      };
    },
    mounted() {
      this.getwarehouseList();
    },
    methods: {
      dateFormat(row, column,cellValue) {
          const daterc = row[column.property]
          if(daterc !=null){
              return moment(cellValue).format("YYYY-MM-DD")
              }
          },
          statusFormat(row, column,cellValue) {
          const daterc = row[column.property]
          if(daterc !=null){
              return cellValue.trim()=="N"?"已同步":"未同步";
              }
        },
      getwarehouseList(){
        this.$http.get('/dictdata/index?dicttypeId=1&page=1&limit=100').then(res => {
          this.warehouseList = res.data.data.records;
        }).catch(e => {
          this.$message.error(e.message);
        });

      },
      openSelect(){
          this.showEdit = true;
        },
        setInvcode(custcode,custname,pk_address,detailinfo)
        {
          this.form.custcode = custcode;
          this.form.custname = custname;
          this.form.pkcustaddr = pk_address;
          this.form.custaddrname = detailinfo;
        },
        editRate()
        {
          if(!this.form.custcode)
          {
            this.$message.info("请先根据配送地址选择客户编码");
            return;
          }
          this.form.erpwarehouse="";
          //console.log(this.form);
          this.$confirm('确定绑定当前平台医院' + this.form.yybm + '】对应ERP客户【' + this.form.custcode+'_'+this.form.custname + '】、收货地址【'+this.form.custaddrname+'】？', '提示', {
          type: 'warning'
        }).then(() => {
          const loading = this.$loading({lock: true});
          this.$http.post('/ygorder/updatecustaddr', this.form).then(res => {
            loading.close();
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              this.reset();
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            loading.close();
            this.$message.error(e.message);
          });
        }).catch(() => {
        });
        },
        handleRowClick(row)
        {
          //Object.assign(this.where,row);
          if(this.selection.length > 0){
          let del_row = this.selection.shift()
          this.$refs.table.toggleRowSelection(del_row,false) //设置这一行取消选中
          }
          this.selection.push(row);
          this.current = row;
          this.$set(this.form,"id",row.id);
          this.$set(this.form,"yybm",row.yybm);
          this.$set(this.form,"yymc",row.yymc);
          this.$set(this.form,"psdbm",row.psdbm);
          this.$set(this.form,"psdz",row.psdz);
          this.$set(this.form,"ywy",row.ywy);
          this.$set(this.form,"pkcustaddr",row.pkcustaddr);
          this.$set(this.form,"custcode",row.custcode);
          this.$set(this.form,"custname",row.custname);
          this.$set(this.form,"custaddrname",row.custaddrname);
          this.$set(this.form,"erpwarehouse",row.erpwarehouse);


        },
      /* 刷新表格 */
      reload() {
        this.where.custaddr = this.form.psdz;
        this.where.nosetflag = this.setFlag
        this.$refs.table.reload({where: this.where});
      },
      /* 重置搜索 */
      reset() {
        this.where = {};
        this.form = {};
        this.reload();
      },
      /* 显示编辑 */
      openEdit(row) {
        this.current = row;
        this.showEdit = true;
      },
      /* 删除 */
      remove(row) {
        const loading = this.$loading({lock: true});
        this.$http.delete('/c37infypyhjl/delete', {id: row.id}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      },
      /* 批量删除 */
      removeBatch() {
        if (!this.selection.length) {
          this.$message.error('请至少选择一条数据');
          return;
        }
        this.$confirm('确定要删除选中的养护记录表吗?', '提示', {
          type: 'warning'
        }).then(() => {
          const loading = this.$loading({lock: true});
          this.$http.delete('/c37infypyhjl/delete', {id: this.selection.map(d => d.id)}).then(res => {
            loading.close();
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              this.reload();
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            loading.close();
            this.$message.error(e.message);
          });
        }).catch(() => {
        });
      },
    }
  }
  </script>

  <style scoped>
    .input-with-select .el-input-group__prepend {
      background-color: #fff;
    }
  </style>
